package com.ganee.agora.view

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.Nullable
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.facebook.FacebookException
import com.facebook.login.LoginResult
import com.ganee.agora.BuildConfig
import com.ganee.agora.R
import com.ganee.agora.manager.FaceBookLoginManager
import com.ganee.agora.manager.GoogleLoginManager
import com.ganee.agora.manager.OnLoginSuccessListener
import com.ganee.agora.model.LoginRequestBody
import com.ganee.agora.viewmodel.LoginViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import com.ganee.baselib.mvvmlibrary.base.HnAppManager
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_login.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.json.JSONException
import org.json.JSONObject
@Route(path = Constance.ACTIVITY_URL_LOGIN)
class LoginActivity : BaseActivity<LoginViewModel>(),View.OnClickListener {

    override fun getViewModel(): LoginViewModel {
        return ViewModelProviders.of(this@LoginActivity).get(LoginViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        ARouter.getInstance().inject(this)
        loginTvOriginal.setOnClickListener(this)
        initGoogle()
        initFacebook()
    }

    private fun initGoogle() {
        loginTvGoogle.setOnClickListener {
            GoogleLoginManager.initGoogle(this@LoginActivity, BuildConfig.GOOGLE_ID, REQUEST_CODE)
        }
    }
    private fun initFacebook() {
        FaceBookLoginManager.getInstance().initFaceBook(this,
                object : OnLoginSuccessListener {
                    override fun onSuccess(result: LoginResult) {
                        val userId = result.accessToken.userId
                        getInfo(userId,"facebook")
                    }

                    override fun onError(ex: FacebookException) {

                    }

                    override fun onCancel() {

                    }

                })

        loginTvFacebook.setOnClickListener {
            FaceBookLoginManager.getInstance().faceBookLogin(this@LoginActivity)
        }
    }

    fun getInfo(userId: String,type:String) {
        //facebook type = 01  google 02
        Log.e("ee",userId +"----" +type)
        val gson = Gson()
        when(type){

            "facebook" -> {
                val loginBody = LoginRequestBody(userId,"01")
                val body = gson.toJson(loginBody)
                val requestBody = RequestBody.create(MediaType.parse("application/json"), body)
                viewModel.login(requestBody)
            }
            "google" -> {
                val requestData = JSONObject()
                try {
                    requestData.put("acctNo", userId)
                    requestData.put("acctType", "02")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                val requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString())
                viewModel.login(requestBody)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE){
            GoogleLoginManager.getInstance().onActivityResult(requestCode, data, REQUEST_CODE, GoogleLoginManager.OnLoginSuccessListener {
                getInfo(it,"google")
            })
        } else {
            FaceBookLoginManager.getInstance().setOnActivityResult(requestCode, resultCode, data)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }
    companion object {
        internal const val REQUEST_CODE = 110
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.loginTvOriginal -> {
                ARouter.getInstance().build(Constance.ACTIVITY_URL_LOGIN_ORIGINAL).navigation()
            }
        }
    }
}

